package org.hank.demoproject.databinding

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.orhanobut.logger.Logger
import org.hank.demoproject.R

/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/4/6 11:38
 *描述:
 */
class DataBindingActivity : AppCompatActivity() {
    lateinit var databinding: ActivityDatabindingBinding
    lateinit var viewModel: DataBindViewModel
    var lastBindStr = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_databinding)
        viewModel = ViewModelProvider(this@DataBindingActivity).get(DataBindViewModel::class.java)
        viewModel.liveEdit.observe(this@DataBindingActivity) {
            Logger.d("liveEdit update $it")
//            if (lastBindStr != it) {
//                databinding.editTest.text = SpannableStringBuilder(it)
//                databinding.editTest.setSelection(it.length)
//            }
//            lastBindStr = it
        }
        databinding.apply {
            item = viewModel
            btnTest.setOnClickListener {
                viewModel.liveEdit.postValue("111")
                databinding.item = viewModel
            }
        }
    }
}